﻿//********************************************************************************//
//创建日期：<创建日期，2016-11-10>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：取水计划下达
//********************************************************************************//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using System.Text;
using Web.Class;
using System.Dynamic;

namespace Web.Pages.QuShui.DiXiaS.JiHuaGL.QuShuiJH
{
    public partial class QuShuiJHXD : BasePage
    {
        #region 属性

        /// <summary>
        /// 保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量
        /// <summary>
        /// 存储过程执行类
        /// </summary>
        SpHelper _SpHelper = new SpHelper();
        BLL.SqlHelper _SplHelper = new BLL.SqlHelper();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        /// 参数bll
        /// </summary>
        QS_CANSHUBBLL _CANSHUBLL = new QS_CANSHUBBLL();

        /// <summary>
        /// 取水计划bll
        /// </summary>
        QS_JH_QUSHUIJHBLL _QSJHBLL = new QS_JH_QUSHUIJHBLL();
        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
            }
            LoadData();
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {
            base.Render(sender);
        }

        /// <summary>
        /// 年份切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 放弃
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            InitData();
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            dynamic expandoObject = new ExpandoObject();
            int year = Convert.ToInt32(cmbNianF1.Value);
            if (e.Parameter == "setinfo")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                if (id != -1)
                {
                    expandoObject.type = "setinfo";
                    DataTable dt_temp = _SplHelper.p_qsjh_jihua_DBS(1, 0, 0, id, year).Tables[0];
                    if (dt_temp.Rows.Count > 0)
                    {
                        expandoObject.QSL = ToWang(dt_temp.Rows[0]["ShenPiL"]);
                        expandoObject.QSL1 = ToWang(dt_temp.Rows[0]["CZSPL1"]);
                        expandoObject.QSL2 = ToWang(dt_temp.Rows[0]["CZSPL2"]);
                        expandoObject.QSL3 = ToWang(dt_temp.Rows[0]["CZSPL3"]);
                        expandoObject.QSL4 = ToWang(dt_temp.Rows[0]["CZSPL4"]);
                        expandoObject.QSL5 = ToWang(dt_temp.Rows[0]["CZSPL5"]);
                        expandoObject.QSL6 = ToWang(dt_temp.Rows[0]["CZSPL6"]);
                        expandoObject.QSL7 = ToWang(dt_temp.Rows[0]["CZSPL7"]);
                        expandoObject.QSL8 = ToWang(dt_temp.Rows[0]["CZSPL8"]);
                        expandoObject.QSL9 = ToWang(dt_temp.Rows[0]["CZSPL9"]);
                        expandoObject.QSL10 = ToWang(dt_temp.Rows[0]["CZSPL10"]);
                        expandoObject.QSL11 = ToWang(dt_temp.Rows[0]["CZSPL11"]);
                        expandoObject.QSL12 = ToWang(dt_temp.Rows[0]["CZSPL12"]);

                        expandoObject.XDJH = ToWang(dt_temp.Rows[0]["XDJH"]);
                        expandoObject.XDL1 = ToWang(dt_temp.Rows[0]["XDL1"]);
                        expandoObject.XDL2 = ToWang(dt_temp.Rows[0]["XDL2"]);
                        expandoObject.XDL3 = ToWang(dt_temp.Rows[0]["XDL3"]);
                        expandoObject.XDL4 = ToWang(dt_temp.Rows[0]["XDL4"]);
                        expandoObject.XDL5 = ToWang(dt_temp.Rows[0]["XDL5"]);
                        expandoObject.XDL6 = ToWang(dt_temp.Rows[0]["XDL6"]);
                        expandoObject.XDL7 = ToWang(dt_temp.Rows[0]["XDL7"]);
                        expandoObject.XDL8 = ToWang(dt_temp.Rows[0]["XDL8"]);
                        expandoObject.XDL9 = ToWang(dt_temp.Rows[0]["XDL9"]);
                        expandoObject.XDL10 = ToWang(dt_temp.Rows[0]["XDL10"]);
                        expandoObject.XDL11 = ToWang(dt_temp.Rows[0]["XDL11"]);
                        expandoObject.XDL12 = ToWang(dt_temp.Rows[0]["XDL12"]);
                    }
                }
            }
            else if (e.Parameter == "save")
            {
                int id = Convert.ToInt32(this.hdCollection.Get("ID"));
                if (id != -1)
                {
                    expandoObject.type = "save";
                    int[] sl = new int[13];
                    ToLiFang(txtQuShuiJH1.Text, out sl[1]);
                    ToLiFang(txtQuShuiJH2.Text, out sl[2]);
                    ToLiFang(txtQuShuiJH3.Text, out sl[3]);
                    ToLiFang(txtQuShuiJH4.Text, out sl[4]);
                    ToLiFang(txtQuShuiJH5.Text, out sl[5]);
                    ToLiFang(txtQuShuiJH6.Text, out sl[6]);
                    ToLiFang(txtQuShuiJH7.Text, out sl[7]);
                    ToLiFang(txtQuShuiJH8.Text, out sl[8]);
                    ToLiFang(txtQuShuiJH9.Text, out sl[9]);
                    ToLiFang(txtQuShuiJH10.Text, out sl[10]);
                    ToLiFang(txtQuShuiJH11.Text, out sl[11]);
                    ToLiFang(txtQuShuiJH12.Text, out sl[12]);

                    int V_Err = 0;
                    string V_Msg = "";
                    string sID = this.hdCollection.Get("ID").ToString();
                    _SplHelper.p_qsjh_jihuasr_dbs(sID, year, 0,
                        sl[1], sl[2], sl[3], sl[4], sl[5], sl[6],
                        sl[7], sl[8], sl[9], sl[10], sl[11], sl[12],
                        "下达", out V_Err, out V_Msg);
                    expandoObject.success = V_Err == 0;
                    expandoObject.msg = V_Msg;
                }
            }
            else if (e.Parameter == "saveBatch")
            {
                expandoObject.type = "save";
                int V_Err = 0;
                string V_Msg = "";
                _SplHelper.p_qsjh_jihuasr_dbs("", year, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "下达", out V_Err, out V_Msg);
                expandoObject.success = V_Err == 0;
                expandoObject.msg = V_Msg;
            }
            e.Result = Newtonsoft.Json.JsonConvert.SerializeObject(expandoObject);
        }

        /// <summary>
        /// 客户端刷新list
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData();
        }

        protected void gvList_CustomColumnDisplayText(object sender, DevExpress.Web.ASPxGridViewColumnDisplayTextEventArgs e)
        {
            if (e.Column.Caption == "序号")
                e.DisplayText = (e.VisibleRowIndex + 1).ToString();
            if (e.Column.FieldName == "CZSPL" || e.Column.FieldName == "XDL")
                e.DisplayText = ToWang(e.Value);
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            // 年份
            int year = DateTime.Now.Year;
            for (int i = year - 10; i <= year; i++)
            {
                cmbNianF1.Items.Add(i.ToString(), i);
            }
            cmbNianF1.SelectedIndex = 10;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            int year = Convert.ToInt32(cmbNianF1.Value);
            int iWorkT = Convert.ToInt32(rblWorktype.SelectedItem.Value);
            this.dtSource = _SplHelper.p_qsjh_jihua_DBS(0, iWorkT, 0, 0, year).Tables[0];
            gvList.DataSource = this.dtSource;
            gvList.DataBind();
            lbcount.Text = "共" + this.dtSource.Rows.Count.ToString() + "条";
        }

        private string ToWang(object val)
        {
            decimal dVal = DBNull.Value == val ? 0 : Convert.ToDecimal(val);
            dVal /= 10000;
            return dVal.ToString();
        }
        private void ToLiFang(string sVal, out int iVal)
        {
            decimal dVal = Convert.ToDecimal(sVal);
            dVal *= 10000;
            iVal =(int)dVal;
        }

        #endregion        
    }
}